<template>
    <div class="info">
        <span style="color:red;">带*是必填项</span>
        <el-descriptions title="企业信息" direction="vertical" :column="4" border style="width:1200px;height:620px"
            v-show="isEnterpriseInfoFormNull">
            <el-descriptions-item label="企业编号*">
                <el-input v-model="enterpriseInfoForm.enterprise_number" disabled></el-input>
            </el-descriptions-item>
            <el-descriptions-item label="企业名称*">
                <el-input v-model="enterpriseInfoForm.enterprise_name" :disabled="ableToUpdate"></el-input>
            </el-descriptions-item>
            <el-descriptions-item label="负责人" :span="2">
                <el-input v-model="enterpriseInfoForm.person_in_charge" :disabled="ableToUpdate"></el-input>
            </el-descriptions-item>
            <el-descriptions-item label="营业执照">
                <el-input v-model="enterpriseInfoForm.business_license" :disabled="ableToUpdate"></el-input>
            </el-descriptions-item>
            <el-descriptions-item label="审核状态">
                <el-tag style="margin-left:100px;width:200px">{{ enterpriseInfoForm.examine_state
                }}</el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="创建时间">
                <el-tag class="el-icon-date" style="width:200px">{{ format(enterpriseInfoForm.create_time).format
                }}</el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="更新时间">
                <el-tag class="el-icon-date" style="width:200px">{{ format(enterpriseInfoForm.update_time).format
                }}</el-tag>
            </el-descriptions-item>
        </el-descriptions>
        <div class="updateBtn" v-show="!isAdd">
            <el-button type="primary" v-show="isUpdate" @click="doNotUpdateEnterpriseInfo">返回</el-button>
            <el-button type="primary" @click="toUpdateEnterpriseInfo"
                v-show="!isUpdate && isEnterpriseInfoFormNull">修改</el-button>
            <el-button type="primary" v-show="isUpdate" @click="doUpdateEnterpriseInfo">提交修改</el-button>
        </div>
        <div v-show="!isEnterpriseInfoFormNull" class="nullTips">
            <span style="color:red;">暂没有企业信息</span><br>
            <el-button type="primary" @click="toAddEnterpriseInfo" style="margin-top:20px">去完善</el-button>
        </div>
        <div v-show="isAdd" class="addBtn">
            <el-button type="primary" @click="doAddEnterpriseInfo">提交</el-button>
        </div>
    </div>
</template>
<script>
import axios from 'axios';
import dateTime from '@/utils/date_time';
export default {
    data() {
        return {
            isAdd: false,
            isEnterpriseInfoFormNull: false,
            isUpdate: false,
            ableToUpdate: true,
            enterpriseInfoForm: {
                school_recruitment_enterprise_id: '',
                enterprise_number: '',
                enterprise_name: '',
                person_in_charge: '',
                business_license: '',
                examine_state: '',
                user_id: window.localStorage.getItem("user_id"),
                create_time: '',
                update_time: '',
            }
        };
    },
    methods: {
        doAddEnterpriseInfo() {
            if (!this.enterpriseInfoForm.enterprise_number) {
                this.$message.error("请输入企业编号!");
                return;
            }
            if (!this.enterpriseInfoForm.enterprise_name) {
                this.$message.error("请输入企业名称!");
                return;
            }
            let that = this;
            axios({
                method: 'post',
                url: 'http://127.0.0.1:5000/api/school_recruitment_enterprise/add',
                data: {
                    user_id: window.localStorage.getItem("user_id"),
                    enterprise_number: that.enterpriseInfoForm.enterprise_number,
                    enterprise_name: that.enterpriseInfoForm.enterprise_name,
                    person_in_charge: that.enterpriseInfoForm.person_in_charge,
                    business_license: that.enterpriseInfoForm.business_license,
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {
                    console.log(response.data);
                    that.$message.success("更新信息成功");
                    window.location.reload();
                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        },
        doUpdateEnterpriseInfo() {
            if (!this.enterpriseInfoForm.enterprise_number) {
                this.$message.error("请输入企业编号!");
                return;
            }
            if (!this.enterpriseInfoForm.enterprise_name) {
                this.$message.error("请输入企业名称!");
                return;
            }
            let that = this;
            axios({
                method: 'post',
                url: 'http://127.0.0.1:5000/api/school_recruitment_enterprise/set?school_recruitment_enterprise_id=' + that.enterpriseInfoForm.school_recruitment_enterprise_id,
                data: {
                    enterprise_number: that.enterpriseInfoForm.enterprise_number,
                    enterprise_name: that.enterpriseInfoForm.enterprise_name,
                    person_in_charge: that.enterpriseInfoForm.person_in_charge,
                    business_license: that.enterpriseInfoForm.business_license,
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {
                    console.log(response.data);
                    that.$message.success("更新信息成功");
                    window.location.reload();
                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        },
        toAddEnterpriseInfo() {
            this.isAdd = true;
            this.isEnterpriseInfoFormNull = true;
            this.ableToUpdate = !this.ableToUpdate;
        },
        doNotUpdateEnterpriseInfo() {
            window.location.reload();
        },
        toUpdateEnterpriseInfo() {
            this.isUpdate = true;
            this.ableToUpdate = false;
        },
        format(create_time) {
            return dateTime.dateTime(create_time)
        }
    },
    created: function () {
        if (!window.localStorage.getItem("token")) {
            this.$router.push({ path: '/login' }, () => { }, () => { });
            return;
        }
        let that = this;
        axios({
            method: 'get',
            url: 'http://127.0.0.1:5000/api/school_recruitment_enterprise/get_obj',
            params: {
                user_id: that.enterpriseInfoForm.user_id,
            },
            headers: {
                'X-Auth-Token': window.localStorage.getItem("token")
            },
        })
            .then(function (response) {
                console.log(response.data);
                if (response.data.result != null) {
                    that.enterpriseInfoForm = response.data.result.obj;
                    that.isEnterpriseInfoFormNull = true;
                }
            })
            .catch(function (error) {
                console.log(error);
            })
            .then(function () {
                // 总是会执行
            })
    }
}
</script>
<style lang="less" scoped>
.info {

    margin-left: 500px;
    margin-top: -400px;
    margin-bottom: 150px;
}

.updateBtn {
    margin-top: -200px;
    margin-left: -300px;
}

.nullTips {
    margin-left: -300px;
    margin-bottom: 550px;
}

.addBtn {
    margin-left: -300px;
    margin-top: -300px;
    margin-bottom: 260px;
}
</style>